package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Db.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import flash.events.*;
    import modules.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene.events.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.common.alertwins.*;
    import mortal.game.view.player.*;
    import mortal.game.view.shop.*;
    import mortal.game.view.uiIconBtn.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PlayerController extends Controller
    {
        public const posType:int = 3;
        private var _playerModule:PlayerModule;
        private var lastRingChangeIndex:int;
        private var lastJewelryChangeIndex:int;
        private var lastCLientRingChangeIndex:int;
        private var lastClientJewelryChangeIndex:int;
        private var hintIcon:AcupointHintIcon;
        private var _login:Boolean = false;

        public function PlayerController()
        {
            NetDispatcher.addCmdListener(ServerCommand.RoleFightAttributeChange, this.updateAttributes);
            NetDispatcher.addCmdListener(ServerCommand.RoleFightAttributeBaseChange, this.updateAttributes);
            NetDispatcher.addCmdListener(ServerCommand.RolepackDataChange, this.updateAllItems);
            NetDispatcher.addCmdListener(ServerCommand.RolePackItemsChange, this.updateAllItems);
            NetDispatcher.addCmdListener(ServerCommand.RoleExpUpdate, this.roleExpUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.EntityInfo_Update, this.updateBaseAttributes);
            NetDispatcher.addCmdListener(ServerCommand.MountsListRes, this.updateMounts);
            NetDispatcher.addCmdListener(ServerCommand.MountsCallRes, this.updateMounts);
            NetDispatcher.addCmdListener(ServerCommand.MountsUnEquipRes, this.updateMounts);
            NetDispatcher.addCmdListener(ServerCommand.MountsEquipRes, this.updateMounts);
            NetDispatcher.addCmdListener(ServerCommand.RoleCareerUpdate, this.onRoleCareerUpdateHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.VIPInfoUpdateCommand, this.onVipInfoUpdateHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAcupointInfo, this.onAcupointInfoReciveHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleNimbusChange, this.onNimbusChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            Dispatcher.addEventListener(EventName.AcupointBeginUpgrade, this.onAcupointBeginUpgradeHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeSuccess, this.onUpgradeSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointSpeedupUpgrade, this.onSpeedupUpgradeHandler);
            Dispatcher.addEventListener(EventName.AcupointReduceUpgradeTimeSuccess, this.onSpeedupUpgradeSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointHintIconClick, this.onHintIconClickHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeRootSuccess, this.onUpgradeRootSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeSavvySuccess, this.onUpgradeSavvySuccessHandler);
            
        }

        private function showHintIcon() : void
        {
            if (this.hintIcon == null)
            {
                this.hintIcon = new AcupointHintIcon();
            }
            this.hintIcon.show();
            UiHintIconView.instance.addHintIcon(this.hintIcon);
            
        }

        private function hideHintIcon() : void
        {
            if (this.hintIcon != null)
            {
                if (this.hintIcon.parent)
                {
                    this.hintIcon.parent.removeChild(this.hintIcon);
                    EffectManager.glowFilterUnReg(this.hintIcon);
                    this.hintIcon.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
                }
            }
            
        }

        public function dress(param1:int, param2:String, param3:int = -1) : void
        {
            var vItemData:ItemData;
            var vIndex:int;
            var clientTargetIndex:int;
            var htmlContent:String;
            var posType:* = param1;
            var uid:* = param2;
            var targetIndex:* = param3;
            if (posType == 0)
            {
                vIndex = Cache.instance.pack.backPackCache.getIndexByUid(uid);
            }
            vIndex = Cache.instance.pack.getPackChacheByPosType(posType).getIndexByUid(uid);
            vItemData = Cache.instance.pack.getPackChacheByPosType(posType).getItemDataByIndex(vIndex);
            if (targetIndex == -1)
            {
                targetIndex = this.getIndex(vItemData, targetIndex);
            }
            if (targetIndex != -1)
            {
                if (vItemData)
                {
                    if (vItemData.itemExInfo && vItemData.itemExInfo.bind == EBind._EBindAfterUse)
                    {
                        var onClickHandler:* = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    GameProxy.roleProxy.dress_async(vItemData, targetIndex, posType, vIndex);
                    Alert.resetToDefault();
                }
                
            }
            ;
                        htmlContent;
                        Alert.backgroundAlpha = 0;
                        Alert.alertWinRenderer = CustomAlertWin;
                        Alert.show(htmlContent, null, Alert.OK | Alert.CANCEL, null, onClickHandler);
                    }
                    else
                    {
                        GameProxy.roleProxy.dress_async(vItemData, targetIndex, posType, vIndex);
                    }
                }
            }
            
        }

        public function unDress(param1:String) : void
        {
            var _loc_2:* = Cache.instance.pack.rolePackCache.getIndexByUid(param1);
            GameProxy.roleProxy.undress_async(param1, _loc_2);
            
        }

        private function mouse_over(event:MouseEvent) : void
        {
            if (Cache.instance.shop.isOpening)
            {
                switch(cache.shop.operType)
                {
                    case ShopOperType.FIX:
                    {
                        CursorManager.showCursor(CursorManager.FIX);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            
        }

        private function mouse_out(event:MouseEvent) : void
        {
            CursorManager.showCursor(CursorManager.NO_CURSOR);
            
        }

        private function getIndex(param1:ItemData, param2:int) : int
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            if (param1 && param1.itemInfo && param1.itemInfo.item)
            {
                _loc_3 = param1.itemInfo.item.type;
                _loc_4 = param1.itemInfo.item.category;
            }
            if (param2 == -1)
            {
                if (_loc_4 == ECategory._ECategoryEquip)
                {
                    if (_loc_3 == EEquip._EEquipWeapon)
                    {
                        param2 = EDressPos._EDressPosWeapon;
                    }
                    else if (_loc_3 == EEquip._EEquipHelmet)
                    {
                        param2 = EDressPos._EDressPosHelmet;
                    }
                    else if (_loc_3 == EEquip._EEquipShoulder)
                    {
                        param2 = EDressPos._EDressPosShoulder;
                    }
                    else if (_loc_3 == EEquip._EEquipClothes)
                    {
                        param2 = EDressPos._EDressPosClothes;
                    }
                    else if (_loc_3 == EEquip._EEquipWristlet)
                    {
                        param2 = EDressPos._EDressPosWristlet;
                    }
                    else if (_loc_3 == EEquip._EEquipGloves)
                    {
                        param2 = EDressPos._EDressPosGloves;
                    }
                    else if (_loc_3 == EEquip._EEquipBelt)
                    {
                        param2 = EDressPos._EDressPosBelt;
                    }
                    else if (_loc_3 == EEquip._EEquipPants)
                    {
                        param2 = EDressPos._EDressPosPants;
                    }
                    else if (_loc_3 == EEquip._EEquipShoes)
                    {
                        param2 = EDressPos._EDressPosShoes;
                    }
                    else if (_loc_3 == EEquip._EEquipRing)
                    {
                        if (!Cache.instance.pack.rolePackCache.getItemDataByIndex(EDressPos._EDressPosRing1))
                        {
                            param2 = EDressPos._EDressPosRing1;
                            this.lastRingChangeIndex = param2;
                        }
                        else if (!Cache.instance.pack.rolePackCache.getItemDataByIndex(EDressPos._EDressPosRing2))
                        {
                            param2 = EDressPos._EDressPosRing2;
                            this.lastRingChangeIndex = param2;
                        }
                        else if (this.lastRingChangeIndex == EDressPos._EDressPosRing1)
                        {
                            param2 = EDressPos._EDressPosRing2;
                            this.lastRingChangeIndex = param2;
                        }
                        else
                        {
                            param2 = EDressPos._EDressPosRing1;
                            this.lastRingChangeIndex = param2;
                        }
                    }
                    else if (_loc_3 == EEquip._EEquipJewelry)
                    {
                        if (!Cache.instance.pack.rolePackCache.getItemDataByIndex(EDressPos._EDressPosJewelry1))
                        {
                            param2 = EDressPos._EDressPosJewelry1;
                            this.lastJewelryChangeIndex = param2;
                        }
                        else if (!Cache.instance.pack.rolePackCache.getItemDataByIndex(EDressPos._EDressPosJewelry2))
                        {
                            param2 = EDressPos._EDressPosJewelry2;
                            this.lastJewelryChangeIndex = param2;
                        }
                        else if (this.lastJewelryChangeIndex == EDressPos._EDressPosJewelry1)
                        {
                            param2 = EDressPos._EDressPosJewelry2;
                            this.lastJewelryChangeIndex = param2;
                        }
                        else
                        {
                            param2 = EDressPos._EDressPosJewelry1;
                            this.lastJewelryChangeIndex = param2;
                        }
                    }
                    else if (_loc_3 == EEquip._EEquipFashion)
                    {
                        param2 = EDressPos._EDressPosFashion;
                    }
                }
                else if (_loc_4 == ECategory._ECategoryMount)
                {
                    param2 = EDressPos._EDressPosMount;
                }
            }
            return param2;
        }

        override protected function initView() : IView
        {
            if (this._playerModule == null)
            {
                this._playerModule = new PlayerModule();
                this._playerModule.x = 300;
                this._playerModule.y = 100;
                this._playerModule.mode = PlayerModule.Mode_Self;
                this._playerModule.addEventListener(Event.ADDED_TO_STAGE, this.initOnAddToAtage);
                this._playerModule.addEventListener(PlayerEquipInfoPanel.Event_Dress, this.onDress);
                this._playerModule.addEventListener(PlayerEquipInfoPanel.Event_unDress, this.onUnDress);
                this._playerModule.addEventListener(MouseEvent.MOUSE_OVER, this.mouse_over);
                this._playerModule.addEventListener(MouseEvent.MOUSE_OUT, this.mouse_out);
            }
            return this._playerModule;
        }

        private function initOnAddToAtage(event:Event) : void
        {
            this.updateBaseAttributes();
            this.roleExpUpdateHandler();
            this.updateAttributes();
            this.updateAllItems();
            this.updateMounts();
            
        }

        private function onDress(event:DataEvent) : void
        {
            this.dress(event.data.posType, event.data.uid, event.data.index);
            
        }

        private function onUnDress(event:DataEvent) : void
        {
            GameProxy.roleProxy.undress_async(event.data.uid, event.data.index);
            
        }

        private function updateMounts(param1:Object = null) : void
        {
            if (this._playerModule == null || this._playerModule.parent == null)
            {
                
            }
            this._playerModule.updateMounts(cache.pack.mountsPackCache.equipEdMount);
            
        }

        private function updateAttributes(param1:Object = null) : void
        {
            if (this._playerModule == null || this._playerModule.parent == null)
            {
                
            }
            if (Cache.instance.role.fightAttribute && Cache.instance.role.fightAttributeBase)
            {
                this._playerModule.updateAttr(Cache.instance.role.fightAttribute, Cache.instance.role.fightAttributeBase, cache.role.entityInfo.career);
                this.updateHeadInfo();
            }
            
        }

        private function roleExpUpdateHandler(param1:Object = null) : void
        {
            if (this._playerModule == null || this._playerModule.parent == null)
            {
                
            }
            this._playerModule.setExperienceValue(cache.role.maxExp, cache.role.roleInfo.experience);
            
        }

        private function updateBaseAttributes(param1:Object = null) : void
        {
            if (this._playerModule == null || this._playerModule.parent == null)
            {
                
            }
            this._playerModule.setLifeValue(cache.role.entityInfo.maxLife, cache.role.entityInfo.life);
            this._playerModule.setMagicValue(cache.role.entityInfo.maxMana, cache.role.entityInfo.mana);
            this.updateHeadInfo();
            
        }

        private function updateHeadInfo() : void
        {
            var _loc_7:String = null;
            if (this._playerModule == null || this._playerModule.parent == null)
            {
                
            }
            var _loc_1:* = cache.role.playerInfo.name;
            var _loc_2:* = cache.role.roleInfo.career;
            var _loc_3:* = cache.role.entityInfo.level;
            var _loc_4:* = cache.role.entityInfo.camp;
            var _loc_5:* = cache.role.money.honour;
            var _loc_6:* = Cache.instance.guild.myGuildInfo;
            if (Cache.instance.guild.myGuildInfo != null)
            {
                _loc_7 = _loc_6.guildName;
            }
            else
            {
                _loc_7 = "无";
            }
            this._playerModule.updateHeadInfo(_loc_1, _loc_2, _loc_3, _loc_4, _loc_5, _loc_7);
            
        }

        private function updateAllItems(param1:Object = null) : void
        {
            if (this._playerModule == null || this._playerModule.parent == null)
            {
                
            }
            this._playerModule.setEquipments(cache.pack.rolePackCache.getAllItems(), cache.role.entityInfo.career);
            
        }

        private function onBeginUpgradeGrowthHandler(event:DataEvent) : void
        {
            GameProxy.playerPulseProxy.startUpgradeGrowth();
            
        }

        private function onRoleCareerUpdateHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as int;
            if (cache.role.entityInfo.level >= 12 && _loc_2 >= ECareer._ECareerWarrior && _loc_2 <= ECareer._ECareerDoctor)
            {
                this.showHintIcon();
            }
            
        }

        private function onRoleLevelUpdateHandler(param1:Object) : void
        {
            var _loc_2:* = cache.role.entityInfo.level;
            var _loc_3:* = cache.role.entityInfo.career;
            if (_loc_2 == 12 && _loc_3 >= ECareer._ECareerWarrior && _loc_3 <= ECareer._ECareerDoctor)
            {
                this.showHintIcon();
            }
            
        }

        private function onVipInfoUpdateHandler(param1:Object) : void
        {
            if (this._playerModule != null)
            {
                this._playerModule.vipInfoUpdate();
            }
            
        }

        private function onAcupointInfoReciveHandler(param1:MessageBlock) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:TAcupointConfig = null;
            var _loc_2:* = param1.messageBase as SAcupointInfo;
            AcupointData.setInfo(_loc_2);
            if (_loc_2.leftTime > 0)
            {
                AcupointData.setSecTimer(_loc_2.leftTime);
            }
            if (this._playerModule != null)
            {
                this._playerModule.updateCountdown();
                if (!this._playerModule.isHide)
                {
                    this._playerModule.setAcupointInfo();
                }
            }
            var _loc_3:* = cache.role.entityInfo.career;
            if (cache.role.entityInfo.level >= 12 && _loc_3 >= ECareer._ECareerWarrior && _loc_3 <= ECareer._ECareerDoctor)
            {
                _loc_4 = cache.role.roleInfo.nimbus;
                _loc_5 = AcupointData.acupointInfo.step;
                _loc_6 = AcupointData.getAcupointCurLevel(_loc_5, _loc_2);
                _loc_7 = AcupointConfig.instance.getAcupointInfo(_loc_6, _loc_5);
                if (_loc_7.nimbus > _loc_4)
                {
                    
                }
                if (this._playerModule != null && this._playerModule.isHide)
                {
                    this.showHintIcon();
                }
                else if (this._playerModule == null && _loc_2.leftTime <= 0)
                {
                    this.showHintIcon();
                }
            }
            
        }

        private function onAcupointBeginUpgradeHandler(event:DataEvent) : void
        {
            GameProxy.acupoint.upgradeAcupoint();
            
        }

        private function onUpgradeSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            AcupointData.setSecTimer(_loc_2);
            if (this._playerModule != null)
            {
                this._playerModule.updateCountdown();
            }
            
        }

        private function onSpeedupUpgradeHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.acupoint.reduceUpgradeAcupoint(_loc_2);
            
        }

        private function onSpeedupUpgradeSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            AcupointData.setSecTimer(_loc_2);
            if (_loc_2 > 0 && this._playerModule != null)
            {
                this._playerModule.updateCountdown();
            }
            
        }

        private function onHintIconClickHandler(event:DataEvent) : void
        {
            view.show();
            this._playerModule.showAcupoint();
            
        }

        private function onNimbusChangeHandler(param1:int) : void
        {
            if (this._playerModule != null && !this._playerModule.isHide)
            {
                this._playerModule.updateNimbus(param1);
            }
            
        }

        private function onMoneyUpdateHandler(param1:Object) : void
        {
            if (this._playerModule != null && !this._playerModule.isHide)
            {
                this._playerModule.updateMoney();
            }
            
        }

        private function onUpgradeRootSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = _loc_2.step;
            var _loc_4:* = _loc_2.newLevel;
            var _loc_5:* = _loc_2.acupointAttribute;
            AcupointData.setRootLevel(_loc_3, _loc_4);
            AcupointData.setAttributesByStep(_loc_3, _loc_5);
            if (this._playerModule != null && !this._playerModule.isHide)
            {
                this._playerModule.updateAcupointInfo(_loc_3);
            }
            
        }

        private function onUpgradeSavvySuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            var _loc_3:* = _loc_2.step as int;
            var _loc_4:* = _loc_2.newLevel as int;
            AcupointData.setSavvyLevel(_loc_3, _loc_4);
            if (this._playerModule != null && !this._playerModule.isHide)
            {
                this._playerModule.updateAcupointInfo(_loc_3);
            }
            
        }

    }
}
